package com.nkzy.controller;

import com.nkzy.pojo.User;
import com.nkzy.pojo.Result;
import com.nkzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    // 插入新用户
    @PostMapping
    public Result<User> insertUser(@RequestBody User user) {
        int result = userService.insertUser(user);
        if (result > 0) {
            return new Result<>(user);
        } else {
            return new Result<>("插入用户失败");
        }
    }

    // 根据ID删除用户
    @DeleteMapping("/{id}")
    public Result<String> deleteUserById(@PathVariable Long id) {
        int result = userService.deleteUserById(id);
        if (result > 0) {
            return new Result<>("用户已删除");
        } else {
            return new Result<>("删除用户失败");
        }
    }

    // 更新用户信息
    @PutMapping
    public Result<User> updateUser(@RequestBody User user) {
        int result = userService.updateUser(user);
        if (result > 0) {
            return new Result<>(user);
        } else {
            return new Result<>("更新用户失败");
        }
    }

    // 根据ID查询用户
    @GetMapping("/{id}")
    public Result<User> selectUserById(@PathVariable Long id) {
        User user = userService.selectUserById(id);
        if (user != null) {
            return new Result<>(user);
        } else {
            return new Result<>("用户未找到");
        }
    }

    // 查询所有用户
    @GetMapping("/list")
    public Result<List<User>> selectAllUsers() {
        List<User> users = userService.selectAllUsers();
        return new Result<>(users);
    }

    // 根据ID列表查询用户
    @GetMapping("/ids")
    public Result<List<User>> selectUsersByIds(@RequestParam List<Long> ids) {
        List<User> users = userService.selectUsersByIds(ids);
        return new Result<>(users);
    }
}